//
//  VKMainPersonView.m
//  V-Learn
//
//  Created by liubo on 2019/3/19.
//  Copyright © 2019 yxk. All rights reserved.
//

#import "VKContactGrifView.h"
@implementation VKContactGrifView

- (instancetype)initWithFrame:(CGRect)frame withDataArray:(NSMutableArray *)dataArray
{
    self = [super initWithFrame:frame];
    if (self) {
        self.dataArray = dataArray;

        [self setupLayout];
    }
    return self;
}
-(void)setDataArray:(NSMutableArray *)dataArray{
    _dataArray = dataArray;
    for (UIView *view in self.subviews) {
        [view removeFromSuperview];
    }
    [self setupLayout];
}

- (void)setupLayout{
    [self setupLayoutViews];
}

-(void)setupLayoutViews{
    
    CGFloat x = 15;
    CGFloat y = 20;
    NSArray *array = @[@"讲道理",@"很少哭闹",@"急躁",@"耍赖皮",@"说话算数",@"界限不清",@"喜欢去上学",@"喜欢去幼儿场",@"喜欢画画",@"喜欢听音乐",@"喜欢手工",@"喜欢阅读",@"社交能力强",@"有自己的好朋友",@"乐于分享"];
    for (int i = 0; i < array.count; i++) {
        
        UILabel *label = [self labelWithTitle:array[i]];
        CGFloat width = label.frame.size.width + 20;
        label.backgroundColor = [UIColor clearColor];
        label.textColor = [UIColor clearColor];
//        label.tag = i;
        if (x + width + 15 > self.bounds.size.width) {
            y += 40;//换行
            x = 15; //15位置开始
        }
        
        label.frame = CGRectMake(x, y, width, 30);
        [self addSubview:label];
        x += width + 10;//宽度+间隙
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [label addSubview:button];
        if ([self.dataArray containsObject:@(i + 1)]) {
            button.selected = YES;
        }
        button.tag = i + 1;
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        [button setTitle:array[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor cz_colorWithRed:117 green:117 blue:117] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(label);
            make.width.equalTo(label.mas_width);
            make.height.equalTo(label.mas_height);
        }];
        if (button.selected) {
            button.backgroundColor = [UIColor cz_colorWithRed:238 green:81 blue:39];//;
        }else{
            button.backgroundColor = [UIColor whiteColor];
        }
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        
    }
}
- (void)buttonClick:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (sender.selected) {
        sender.backgroundColor = [UIColor cz_colorWithRed:238 green:81 blue:39];
        [self.dataArray addObject:@(sender.tag)];
    }else{
        [self.dataArray removeObject:@(sender.tag)];
        sender.backgroundColor = [UIColor whiteColor];
    }
//    NSLog(@"self.dataArray:%@",self.dataArray);

}


- (UILabel *)labelWithTitle:(NSString *)title{
    
    UILabel *label = [[UILabel alloc] init];
    label.text = title;
    [label sizeToFit];
    label.layer.cornerRadius = 6;
    label.layer.borderWidth = 1;
    label.layer.borderColor = [UIColor cz_colorWithRed:227 green:227 blue:227].CGColor;
    label.layer.masksToBounds = YES;
    label.userInteractionEnabled = YES;
    label.font = [UIFont systemFontOfSize:15];
    label.textAlignment = NSTextAlignmentCenter;
    return label;
}


@end
